using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;
using MoveTheCar.ViewModel;

namespace MoveTheCar.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : SurfaceWindow
    {
        private MainViewModel mvm;
        private Rectangle rectangle;
        private Point originPoint;

        /// <summary>
        /// Default constructor.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            // Add handlers for window availability events
            AddWindowAvailabilityHandlers();
        }

        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for window availability events
            RemoveWindowAvailabilityHandlers();
        }

        /// <summary>
        /// Adds handlers for window availability events.
        /// </summary>
        private void AddWindowAvailabilityHandlers()
        {
            // Subscribe to surface window availability events
            ApplicationServices.WindowInteractive += OnWindowInteractive;
            ApplicationServices.WindowNoninteractive += OnWindowNoninteractive;
            ApplicationServices.WindowUnavailable += OnWindowUnavailable;
        }

        /// <summary>
        /// Removes handlers for window availability events.
        /// </summary>
        private void RemoveWindowAvailabilityHandlers()
        {
            // Unsubscribe from surface window availability events
            ApplicationServices.WindowInteractive -= OnWindowInteractive;
            ApplicationServices.WindowNoninteractive -= OnWindowNoninteractive;
            ApplicationServices.WindowUnavailable -= OnWindowUnavailable;
        }

        /// <summary>
        /// This is called when the user can interact with the application's window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowInteractive(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
        }

        /// <summary>
        /// This is called when the user can see but not interact with the application's window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowNoninteractive(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled

            //TODO: optionally enable animations here
        }

        /// <summary>
        /// This is called when the application's window is not visible or interactive.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowUnavailable(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
        }

        #region Events
        private new void MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left && e.OriginalSource.GetType() == typeof(Rectangle))
            {
                if (((Rectangle)e.OriginalSource).Fill == Brushes.Gray)
                    return;

                rectangle = (Rectangle)e.OriginalSource;
                originPoint = e.GetPosition(rectangle);
            }
        }
        private new void MouseMove(object sender, MouseEventArgs e)
        {
            if (mvm == null)
            {
                var dm = DataContext;
                mvm = dm as MainViewModel;
            }
            if (rectangle != null && e.LeftButton == MouseButtonState.Pressed)
            {
                Point movePoint = e.GetPosition(rectangle);
                Point newLoc = new Point();
                newLoc.X = movePoint.X - originPoint.X;
                newLoc.Y = movePoint.Y - originPoint.Y;
                mvm.MoveCar(newLoc, rectangle);
            }
        }
        private new void MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left && rectangle != null)
                rectangle = null;
        }

        private void Touch_Move(object sender, TouchEventArgs e)
        {
            if (mvm == null)
            {
                var dm = DataContext;
                mvm = dm as MainViewModel;
            }
            if (e.OriginalSource.GetType() == typeof(Rectangle))
            {
                if (((Rectangle)e.OriginalSource).Fill == Brushes.Gray)
                    return;

                TouchPointCollection tpoints = e.GetIntermediateTouchPoints(this);
                Point newLoc = new Point();
                newLoc.X = tpoints[tpoints.Count - 1].Position.X - tpoints[0].Position.X;
                newLoc.Y = tpoints[tpoints.Count - 1].Position.Y - tpoints[0].Position.Y;
                mvm.MoveCar(newLoc, (Rectangle)e.OriginalSource);
            }
        }
        #endregion Events
    }
}